// Simple Alarm Tree representation for logging/demo
#pragma once

#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QMap>
#include <QtCore/QSharedPointer>
#include <QtCore/QStringList>

namespace qtc_alarm_core {

struct AlarmEvent; // forward decl

class AlarmTreeNode {
public:
    QString name;
    QString severity {"OK"};
    int pv_count {0};
    QMap<QString, QSharedPointer<AlarmTreeNode>> children;

    AlarmTreeNode() = default;
    explicit AlarmTreeNode(const QString& name) : name(name) {}

    QJsonObject toJson() const;
};

class AlarmTree {
public:
    explicit AlarmTree(const QString& root_name = "Root");
    void applyEvent(const AlarmEvent& ev);
    QJsonObject toJson() const;

private:
    QSharedPointer<AlarmTreeNode> root;
    static QString worstSeverity(const QString& a, const QString& b);
    QSharedPointer<AlarmTreeNode> ensurePath(const QStringList& parts);
};

} // namespace qtc_alarm_core